#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "netdtu.h"
#include "dtuhardware.h"
#include "NT1.h"
#include "wifiap.h"
#include "utils.h"

#define RS485_RX_TASK_STACK_SIZE 1024 * 6
#define RS485_RX_TASK_PRIO 28
#define RS485_UART_BUFF_SIZE 16
#define RS485_UART_STR_SIZE 400
#define RS485_UART_Data_SIZE 400
#define DTU_TIP_LED 9

#define DTU_FLAGS_MSK2 0x00000002U

osEventFlagsId_t DTU_ConfigMsgEvtID;
char DTU_ConfigWIFIData[400] = {0};
int DTU_ConfigWIFIDataLength = 0;
char * DTU_uartData;
int DTU_uartLength = 0;


osThreadAttr_t RS485_attr;
uint8_t RS485_uartBuff[RS485_UART_BUFF_SIZE];
uint8_t * RS485_uartBuffPtr = RS485_uartBuff;

static void *RS485_UART_Task(const char *arg){
    (void)arg;

    printf("[HMNetDTU]RS485_UART_Task start\r\n");

    while (1){
        int rs485_read_length = IoTUartRead(0, RS485_uartBuffPtr, RS485_UART_BUFF_SIZE);
        if(rs485_read_length > 0){
            IoTUartWrite(2, RS485_uartBuffPtr, rs485_read_length);
        }
        osDelay(200);
        memset(RS485_uartBuffPtr,0,RS485_UART_BUFF_SIZE);
    }

    return NULL;
}

static void *service_Task(const char *arg){
    (void)arg;

    printf("[HMNetDTU]Service_Task start\r\n");

    return NULL;
}

void DRU_led(int time){
    for(int i=0;i<10;i++){
        IoTGpioSetOutputVal(DTU_TIP_LED, 0);
        osDelay(time);
        IoTGpioSetOutputVal(DTU_TIP_LED, 1);
    }
}


static void dtuInit(void){

    // 主程序启动
    printf("[HMNetDTU]NetDTU INIT START....\r\n");
    osDelay(10);
    // 初始化HI3861硬件
    LED_INIT();
    KEY_INIT();
    I2C_INIT();
    UART_INIT();
    osDelay(10);
    printf("[HMNetDTU]NetDTU RUN,Log in Uart1,V1.2.6....\r\n");


    // 初始化外围I2C硬件
    DTU_MCP23017_INIT();
    osDelay(50);


    // 启动模块配置程序
    NT1_ConfigThread();  // 启动配置任务
    DTU_ConfigMsgEvtID = osEventFlagsNew(NULL);
    startConfigWifiAp(DTU_ConfigMsgEvtID,DTU_FLAGS_MSK2,(char *)DTU_ConfigWIFIData,&DTU_ConfigWIFIDataLength);  // 开启配置AP

    // 初始化485接口任务
    RS485_attr.name = "RS485_UART_Task";
    RS485_attr.attr_bits = 0U;
    RS485_attr.cb_mem = NULL;
    RS485_attr.cb_size = 0U;
    RS485_attr.stack_mem = NULL;
    RS485_attr.stack_size = RS485_RX_TASK_STACK_SIZE;
    RS485_attr.priority = RS485_RX_TASK_PRIO;
    if (osThreadNew((osThreadFunc_t)RS485_UART_Task, NULL, &RS485_attr) == NULL) {
        mm_LogPrint("[HMNetDTU] Falied to create RS485_UART_Task!\n");
    }    

    // 初始化结束
    printf("[HMNetDTU]NetDTU INIT END....\r\n");
    DRU_led(20);
    
    
    while (1){
        osEventFlagsClear(DTU_ConfigMsgEvtID,DTU_FLAGS_MSK2);
        osEventFlagsWait(DTU_ConfigMsgEvtID, DTU_FLAGS_MSK2, osFlagsWaitAny, 2000);
        // printf("GET wifi data:%s\r\n",(char *)DTU_ConfigWIFIData);
        // 发给串口，等待接收
        if(DTU_ConfigWIFIDataLength > 0){
            mm_LogPrint("[HMNetDTU]GET WIFI data:%s,length:%d \r\n",(char *)DTU_ConfigWIFIData,DTU_ConfigWIFIDataLength);
            DTU_uartLength = 0;

            // 进入配置模式
            IoTUartWrite(2, "+++", 3);
            osDelay(40);
            IoTUartWrite(2, "AT", 2);
            osDelay(70);

            DTU_uartData = NT1_SendMsg((char *)DTU_ConfigWIFIData,&DTU_uartLength,DTU_ConfigWIFIDataLength);
            mm_LogPrint("[DM4GDTU]GET UART data:%s|%d\r\n",(char *)DTU_uartData,DTU_uartLength);
            WIFI_send_data(DTU_uartData,DTU_uartLength);

            // 退出配置模式
            osDelay(20);
            IoTUartWrite(2, "AT+EXAT\r\n", 9);

            memset(DTU_ConfigWIFIData,0,400);
            DTU_ConfigWIFIDataLength = 0;
        }
    }

}

SYS_RUN(dtuInit);